package com.hsm.controller;

import com.hsm.constant.MessageConstant;
import com.hsm.entity.Result;
import com.hsm.pojo.OrderSetting;
import com.hsm.service.OrderSettingService;
import com.hsm.utils.POIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Autowired
    private OrderSettingService orderSettingService;

    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile){
        try {
            //1.使用POI解析文件 得到List<String[]> list
            List<String[]> list = POIUtils.readExcel(excelFile);

            //2.把List<String[]> list转成 List<OrderSetting> list
            if(!CollectionUtils.isEmpty(list)){
                List<OrderSetting> orderSettingList = new ArrayList<>();
                for (String[] strings : list) {
                    OrderSetting orderSetting =
                            new OrderSetting(new Date(strings[0]),Integer.parseInt(strings[1]));
                    orderSettingList.add(orderSetting);
                }
                orderSettingService.add(orderSettingList);
            }
        } catch (IOException e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.IMPORT_ORDERSETTING_FAIL);
        }
        //3.调用业务 进行保存
        return new Result(true, MessageConstant.IMPORT_ORDERSETTING_SUCCESS);
    }
}
